﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using NUnit.Framework;
using TypeResolve.Loader_Net45;
using TypeResolve.Tests.TestClasses;

namespace TypeResolve.Tests
{
	[TestFixture]
	public class test_TypeNameResolver
	{
		[SetUp]
		public void SetUp()
		{
			AssemblyLoader.ConfigurePlatformBasedAssemblyLoading();
		}

		[Test]
		public void ToShortTypeName()
		{
			CheckType(typeof(int), "int");
			CheckType(typeof(int[]), "int[1]");
			CheckType(typeof(int[,]), "int[2]");
			CheckType(typeof(int[][]), "int[1][1]");
			CheckType(typeof(List<int>), "List<int>");
			CheckType(typeof(List<List<int>>), "List<List<int>>");
			CheckType(typeof(Dictionary<int, string>), "Dictionary<int,string>");
			CheckType(typeof(List<int[]>), "List<int[1]>");
			CheckType(typeof(List<>), "List`1");
			CheckType(typeof(Type), "Type");
			CheckType(typeof(KlasseMitGenerics<int, string, bool>), "TypeResolve.Tests.TestClasses.KlasseMitGenerics<int,string,bool>");
			CheckType(typeof(KlasseMitGenerics<int, string, bool>.InnereKlasse1), "TypeResolve.Tests.TestClasses.KlasseMitGenerics`3+InnereKlasse1<int,string,bool>");
			CheckType(typeof(KlasseMitGenerics<int, string, bool>.InnereKlasse2<long>), "TypeResolve.Tests.TestClasses.KlasseMitGenerics`3+InnereKlasse2`1<int,string,bool,long>");


			//Assert.AreEqual("List<int>", TypeResolver.TypeToString(typeof(List<int>), null));
			//Assert.AreEqual("List<List<int>>", TypeResolver.TypeToString(typeof(List<List<int>>), null));
			//Assert.AreEqual("Dictionary<int,string>", TypeResolver.TypeToString(typeof(Dictionary<int, string>), null));
			//Assert.AreEqual("List<int[1]>", TypeResolver.TypeToString(typeof(List<int[]>), null));
			//Assert.AreEqual("List<int>[1]", TypeResolver.TypeToString(typeof(List<int>[]), null));
			//Assert.AreEqual("List<int[3]>", TypeResolver.TypeToString(typeof(List<int[, ,]>), null));
			//Assert.AreEqual("TypeResolve.Tests.TestClasses.KlasseMitGenerics<int,string,bool>", TypeResolver.TypeToString(typeof(KlasseMitGenerics<int, string, bool>), null));
			//Assert.AreEqual("TypeResolve.Tests.TestClasses.KlasseMitGenerics`3+InnereKlasse1<int,string,bool>", TypeResolver.TypeToString(typeof(KlasseMitGenerics<int, string, bool>.InnereKlasse1), null));
			//Assert.AreEqual("TypeResolve.Tests.TestClasses.KlasseMitGenerics`3+InnereKlasse2`1<int,string,bool,long>", TypeResolver.TypeToString(typeof(KlasseMitGenerics<int, string, bool>.InnereKlasse2<long>), null));
			//Assert.AreEqual("List`1", TypeResolver.TypeToString(typeof(List<>), null));
			//Assert.AreEqual("Type", TypeResolver.TypeToString(typeof(Type), null));
		}

		private static void CheckType(Type type, string expectedName)
		{
			var typeName = type.FullName;
			Console.WriteLine(typeName + " -> " + expectedName + " -> " + TypeNameConverter.ToShortTypeName(typeName));
			Assert.AreEqual(expectedName, TypeNameConverter.ToShortTypeName(typeName));
		}

		[Test]
		public void SplitByBrackets()
		{
			//Arrange
			var str = "[[],[,,],],[,,,],[[[]]],[]";

			//Act
			var result = TypeNameConverter.SplitByBrackets(str, ',', '[', ']').ToList();

			//Assert
			Assert.That(result, Is.EquivalentTo(new[] { "[[],[,,],]", "[,,,]", "[[[]]]", "[]" }));
		}
	}
}
